# Base-image
FROM registry.opensuse.org/opensuse/bci/python:3.9@sha256:47094e244334645440158a27e3e1dcebccd6bcb18d973d168eb3cdfc649632a3 AS base

ARG GITHUB_URL=https://github.com
ARG NODEJS_DIST_URL
ARG NPM_REPO
ARG OPENSUSE_CODECS_REPO
ARG OPENSUSE_REPO
ARG PIP_CONFIG

ENV CC=gcc \
    LANG=en_US.UTF-8 \
    LANGUAGE=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8 \
    PYTHONPATH=/opt/pypi \
    npm_config_python=/usr/bin/python3.9
ENV PATH=${PATH}:/usr/local/bin:${PYTHONPATH}/bin

RUN set -e; \
    if [ -n "$NODEJS_DIST_URL" ]; then \
      export "NVM_NODEJS_ORG_MIRROR=$NODEJS_DIST_URL"; \
      printf "disturl=$NODEJS_DIST_URL\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$NPM_REPO" ]; then \
      export "COREPACK_NPM_REGISTRY=$NPM_REPO"; \
      printf "registry=$NPM_REPO\n@jsr:registry=$NPM_REPO\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$OPENSUSE_REPO" ]; then \
      sed -i "s|http://codecs.opensuse.org|$OPENSUSE_CODECS_REPO|g" /etc/zypp/repos.d/*; \
      sed -i "s|http://download.opensuse.org|$OPENSUSE_REPO|g" /etc/zypp/repos.d/*; \
      zypper --no-gpg-checks refresh; \
    fi; \
    if [ -n "$PIP_CONFIG" ]; then \
      mkdir $HOME/.pip/; \
      printf "$PIP_CONFIG" > $HOME/.pip/pip.conf; \
    fi; \
    ARCH_NAME="$(rpm --eval '%{_arch}')"; \
    case "${ARCH_NAME##*-}" in \
      'amd64' | 'x86_64') \
        GOBIN_VERSION='amd64'; \
        ;; \
      'aarch64' | 'arm64') \
        GOBIN_VERSION='arm64'; \
        ;; \
      *) \
        echo >&2 "error: unsupported architecture: '$ARCH_NAME'"; \
        exit 1 \
        ;; \
    esac \
 && zypper refresh \
 && zypper \
      --non-interactive \
        update \
 && zypper \
      --non-interactive \
        install \
        -l \
        --no-recommends \
          bzip2 \
          findutils \
          gawk \
          gcc13 \
          gcc13-c++ \
          gcc13-fortran \
          git-core \
          java-25-openjdk-devel \
          libxml2-devel \
          make \
          nodejs22 \
          nodejs22-devel \
          npm22 \
          python39 \
          python39-devel \
          python39-pip \
          unzip \
          wget \
          zip \
 && update-alternatives --install /usr/bin/cc cc /usr/bin/gcc-13 10 \
 && update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-13 10 \
 && update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-13 10 \
 && update-alternatives --install /usr/bin/gfortran gfortran /usr/bin/gfortran-13 10 \
 && update-alternatives --install /usr/bin/python python /usr/bin/python3.9 10 \
 && update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 10 \
 && mkdir ${PYTHONPATH} \
 && python -m pip install \
      --no-cache-dir \
      --upgrade \
        pip \
        setuptools \
        virtualenv \
 && python -m pip install \
      --no-cache-dir \
      --target ${PYTHONPATH} \
      --upgrade \
        pipenv \
        poetry \
        uv \
 && npm install -g npm \
 && node -v \
 && npm -v \
 && npm install -g corepack \
 && poetry --version \
 && pipenv --version \
 && gcc --version \
 && curl -L --output /usr/bin/bazel \
      $GITHUB_URL/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-${GOBIN_VERSION} \
 && chmod +x /usr/bin/bazel \
 && BAZELISK_BASE_URL=$GITHUB_URL/bazelbuild/bazel/releases/download bazel --version \
 && rm -rf $HOME/.npmrc $HOME/.pip \
 && zypper clean -a \
 && if [ -n "$OPENSUSE_REPO" ]; then \
      sed -i "s|$OPENSUSE_CODECS_REPO|http://codecs.opensuse.org|g" /etc/zypp/repos.d/*; \
      sed -i "s|$OPENSUSE_REPO|http://download.opensuse.org|g" /etc/zypp/repos.d/*; \
    fi

CMD ["/bin/bash"]



# cdxgen-image
FROM base AS cdxgen

ENV CDXGEN_IN_CONTAINER=true \
    CRYPTOGRAPHY_DONT_BUILD_RUST=1 \
    NODE_COMPILE_CACHE="/opt/cdxgen-node-cache" \
    PATH=${PATH}:/opt/cdxgen/node_modules/.bin \
    SAFE_PIP_INSTALL=true

COPY . /opt/cdxgen

RUN set -e; \
    if [ -n "$NODEJS_DIST_URL" ]; then \
      export "NVM_NODEJS_ORG_MIRROR=$NODEJS_DIST_URL"; \
      printf "disturl=$NODEJS_DIST_URL\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$NPM_REPO" ]; then \
      export "COREPACK_NPM_REGISTRY=$NPM_REPO"; \
      printf "registry=$NPM_REPO\n@jsr:registry=$NPM_REPO\n" >> $HOME/.npmrc; \
    fi; \
    if [ -n "$OPENSUSE_REPO" ]; then \
      sed -i "s|http://codecs.opensuse.org|$OPENSUSE_CODECS_REPO|g" /etc/zypp/repos.d/*; \
      sed -i "s|http://download.opensuse.org|$OPENSUSE_REPO|g" /etc/zypp/repos.d/*; \
      zypper --no-gpg-checks refresh; \
    fi; \
    if [ -n "$PIP_CONFIG" ]; then \
      mkdir $HOME/.pip/; \
      printf "$PIP_CONFIG" > $HOME/.pip/pip.conf; \
    fi; \
    zypper \
      --non-interactive \
        install \
        --allow-downgrade \
        -l \
        --no-recommends \
          clang15 \
          graphviz-devel \
          lapacke-devel \
          libjpeg62-devel \
          libmariadb-devel \
          libopenblas_pthreads-devel \
          libopenssl-devel \
          llvm15 \
          llvm15-devel \
          postgresql-devel \
          postgresql-server-devel \
          readline-devel \
 && cd /opt/cdxgen \
 && corepack enable \
 && corepack pnpm install:prod \
 && corepack pnpm cache delete \
 && mkdir -p ${NODE_COMPILE_CACHE} \
 && node /opt/cdxgen/bin/cdxgen.js --help \
 && rm -rf $HOME/.npmrc $HOME/.pip /root/.cache/node \
 && zypper clean -a \
 && chmod a-w -R /opt \
 && if [ -n "$OPENSUSE_REPO" ]; then \
      sed -i "s|$OPENSUSE_CODECS_REPO|http://codecs.opensuse.org|g" /etc/zypp/repos.d/*; \
      sed -i "s|$OPENSUSE_REPO|http://download.opensuse.org|g" /etc/zypp/repos.d/*; \
    fi

WORKDIR /app

ENTRYPOINT ["node", "/opt/cdxgen/bin/cdxgen.js"]
